package com.boatAnchorAssistant.pojo.validation;

import java.text.MessageFormat;
import java.util.Map;

/**
 * ValidationResult
 *
 * @author linkanyway
 * @version 1.0
 * @date 2022/05/30 20:03
 */
public class ValidationResult {
    /**
     * has error
     */
    private boolean error;

    /**
     * map of error messages
     */
    private Map<String, String> errorMsg;

    /**
     * has error
     */
    public boolean hasError() {
        return error;
    }

    /**
     * get the message after formatted
     *
     * @return the error message split by \n
     */
    public String getMessage() {
        if (errorMsg == null || errorMsg.isEmpty ()) {
            return "";
        }
        StringBuilder message = new StringBuilder ();
        errorMsg.forEach ((key, value) -> message.append (MessageFormat.format ("{0}:{1} \n", key, value)));
        return message.toString ();
    }


    /**
     * set error
     *
     * @param error validation result
     */
    protected void setError(boolean error) {
        this.error = error;
    }


    /**
     * get map of error message
     *
     * @return map of error message
     * @see Map
     */
    public Map<String, String> getErrorMsg() {
        return errorMsg;
    }

    /**
     * set map of error message
     *
     * @param errorMsg the map for error messages
     */
    protected void setErrorMsg(Map<String, String> errorMsg) {
        this.errorMsg = errorMsg;
    }

    /**
     * toString
     *
     * @return toString
     */
    @Override
    public String toString() {
        return "ValidationResult{" + "hasErrors=" + error + ", errorMsg=" + errorMsg + '}';
    }

}
